package com.li.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.annotation.Log;
import com.li.enums.BusinessType;
import com.li.result.Result;
import com.li.service.SysUserService;
import com.li.system.SysUser;
import com.li.utils.MD5;
import com.li.vo.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xiaoli
 * @since 2022-10-29
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("用户列表分页查询")
    @GetMapping("/{page}/{limit}")
    public Result list(@PathVariable Long page, @PathVariable Long limit,
                       SysUserQueryVo sysUserQueryVo) {
        Page<SysUser> pageParam = new Page<>(page, limit);
        IPage<SysUser> pageModel = sysUserService.selectPage(pageParam, sysUserQueryVo);
        return Result.ok(pageModel);
    }
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("根据id查询")
    @GetMapping("getUser/{id}")
    public Result query(@PathVariable String id) {
        SysUser user = sysUserService.getById(id);
        return Result.ok(user);
    }

    @Log(title = "用户管理(修改)",businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation("修改用户")
    @PostMapping("/update")
    public Result update(@RequestBody SysUser sysUser) {
        boolean update = sysUserService.updateById(sysUser);
        if (update) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Log(title = "用户管理(添加)",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Result save(@RequestBody SysUser sysUser) {
        //把输入的密码进行加密  MD5
        String password = MD5.encrypt(sysUser.getPassword());
        sysUser.setPassword(password);

        boolean save = sysUserService.save(sysUser);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Log(title = "用户管理(删除)",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation("删除用户")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable String id) {
        boolean remove = sysUserService.removeById(id);
        if (remove) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("更改用户状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable String id, @PathVariable Integer status) {
        sysUserService.updateStatus(id, status);
        return Result.ok();
    }


}

